<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <title>传智健康</title>
    <meta content="传智健康" name="description">
    <meta content="传智健康" name="keywords">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link href="../plugins/elementui/index.css" rel="stylesheet">
    <link href="../plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <link href="../css/style.css" rel="stylesheet">
    <!-- 引入组件库 -->
    <script src="../js/jquery.min.js" type="text/javascript"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置
            <small>用户管理</small>
        </h1>
        <el-breadcrumb class="breadcrumb" separator-class="el-icon-arrow-right">
            <!--            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>-->
            <el-breadcrumb-item :to="{ path: '/' }"><a href="/pages/main.html">首页</a></el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input @keyup.enter.native="handleFilter" class="filter-item" placeholder="用户/电话"
                          style="width: 200px;" v-model="pagination.queryString"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button @click="handleCreate()" class="butT" type="primary">新建</el-button>
            </div>
            <el-table :data="dataList" current-row-key="id" highlight-current-row size="small" stripe>
                <el-table-column align="center" label="序号" type="index"></el-table-column>
                <el-table-column align="center" label="用户" prop="username"></el-table-column>
                <!--<el-table-column align="center" label="密码" prop="password"></el-table-column>-->
                <el-table-column align="center" label="电话" prop="telephone"></el-table-column>
                <el-table-column align="center" label="性别">
                    <template slot-scope="scope">
                        <span>{{ scope.row.gender == '0' ? '不限' : scope.row.gender == '1' ? '男' : '女'}}</span>
                    </template>
                </el-table-column><!--{{new Date(dataList.birthday).toLocaleString()}}:formatter="formatDate"-->
                <el-table-column align="center" label="生日" prop="birthday"></el-table-column>
                <el-table-column align="center" label="状态">
                    <template slot-scope="scope">
                        <span>{{ scope.row.station == '0' ? '启用' : scope.row.station == '1' ? '停用' : '启用'}}</span>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="备注" prop="remark"></el-table-column>
                <el-table-column align="center" label="操作">
                    <template slot-scope="scope">
                        <el-button @click="handleUpdate(scope.row)" size="mini" type="primary">编辑</el-button>
                        <el-button @click="handleDelete(scope.row)" size="mini" type="danger">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        :total="pagination.total"
                        @current-change="handleCurrentChange"
                        class="pagiantion"
                        layout="total, prev, pager, next, jumper">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog :visible.sync="dialogFormVisible" title="新增用户">
                    <template>
                        <el-tabs type="card" v-model="activeName">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form :model="formData" :rules="rules" label-position="right" label-width="100px"
                                         ref="dataAddForm">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户" prop="username">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="密码" prop="password">
                                                <el-input v-model="formData.password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="电话">
                                                <el-input v-model="formData.telephone"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-select v-model="formData.gender">
                                                    <el-option label="不限" value="0"></el-option>
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="生日">
                                                <el-date-picker type="date" placeholder="选择生日"
                                                                v-model="formData.birthday"></el-date-picker>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="状态">
                                                <el-select v-model="formData.station">
                                                    <el-option label="启用" value="0"></el-option>
                                                    <el-option label="停用" value="1"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="备注">
                                                <el-input type="textarea" v-model="formData.remark"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" :value="c.id" type="checkbox" v-model="roleIds">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div class="dialog-footer" slot="footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button @click="handleAdd()" type="primary">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog :visible.sync="dialogFormVisible4Edit" title="编辑用户">
                    <template>
                        <el-tabs type="card" v-model="activeName">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form :model="formData" :rules="rules" label-position="right" label-width="100px"
                                         ref="dataEditForm">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户" prop="username">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="密码" prop="password">
                                                <el-input v-model="formData.password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="电话">
                                                <el-input v-model="formData.telephone"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-select v-model="formData.gender">
                                                    <el-option label="不限" value="0"></el-option>
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="生日">
                                                <el-date-picker type="date" placeholder="选择生日"
                                                                v-model="formData.birthday"></el-date-picker>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="状态">
                                                <el-select v-model="formData.station">
                                                    <el-option label="启用" value="0"></el-option>
                                                    <el-option label="停用" value="1"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="备注">
                                                <el-input type="textarea" v-model="formData.remark"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" :value="c.id" type="checkbox" v-model="roleIds">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div class="dialog-footer" slot="footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button @click="handleEdit()" type="primary">确定</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>

<script>
    let vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//新增和编辑表单中对应的检查项列表数据
            roleIds: [],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏
            rules: {//校验规则
                username: [
                    {required: true, message: '用户为4到16位的字符', trigger: 'blur'},
                    {min: 4, max: 16, message: '用户为4到16位的字符', trigger: 'blur'}
                ],
                password: [
                    {required: true, message: '密码为4到16位的字符', trigger: 'blur'},
                    {min: 4, max: 16, message: '密码为4到16位的字符', trigger: 'blur'}
                ]
            }
        },
        created() {
            this.findPage();
        },
        methods: {
            //权限不足提示消息
            showMessage(r) {
                if (r == "Error: Request failed with status code 403") {
                    this.$message.error("当前用户权限不足");
                } else {
                    this.$message.error("未知错误");
                }
            },
            //编辑
            handleEdit() {
                //进行表单校验
                this.$refs['dataEditForm'].validate((valid) => {
                    if (valid) {
                        //表单校验通过，可以提交数据
                        axios.post("/user/edit.do?roleIds=" + this.roleIds, this.formData).then((res) => {
                            if (res.data.flag) {
                                //弹出成功提示信息
                                this.$message({
                                    type: 'success',
                                    message: res.data.message
                                });
                            } else {
                                this.$message.error(res.data.message);
                            }
                        }).finally(() => {
                            //隐藏编辑窗口
                            this.dialogFormVisible4Edit = false;
                            //不管成功还是失败，都调用分页查询方法
                            this.findPage();
                        });
                    } else {
                        //表单校验不通过
                        this.$message.error("表单数据校验失败!");
                        return false;
                    }
                });
            },
            //添加
            handleAdd() {
                //进行表单校验
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid) {
                        //表单校验通过，发生ajax请求，将录入的数据提交到后台进行处理
                        console.log(this.formData);
                        axios.post("/user/add.do?roleIds=" + this.roleIds, this.formData).then((res) => {
                            //关闭新增窗口
                            this.dialogFormVisible = false;
                            if (res.data.flag) {//执行成功
                                //新增成功后，重新调用分页查询方法，查询出最新的数据
                                this.findPage();
                                //弹出提示信息
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                            } else {//执行失败
                                //弹出提示
                                this.$message.error(res.data.message);
                            }
                        });
                    } else {
                        //校验不通过
                        this.$message.error("数据校验失败，请检查你的输入信息是否正确！");
                        return false;
                    }
                });
            },
            //分页查询
            findPage() {
                let params = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/user/findPage.do", params).then(res => {
                    this.pagination.total = res.data.total;
                    let bir = res.data.rows.birthday;
                    this.dataList = this.formatDate(res.data.rows);
                    // this.dataList = res.data.rows;
                });
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.tableData = {};
                this.roleIds = [];
            },
            formatDate(rows) {
                for (i = 0; i < rows.length; i++) {
                    if (rows[i].birthday != null) {
                        rows[i].birthday = rows[i].birthday.substr(0, 10);
                    }
                }
                return rows;
            },
            /*formatDate(row, column, cellValue){
                //return row.date == "2016-05-04" ? '男' : row.sex == 0 ? '女' : '未知';
                //格式化是针对表格内容即使data数据
                var newDate = row.date== "" ? "-" : row.date.replace(/-/g,"/");
                return newDate
            },*/
            // 弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                this.activeName = 'first';
                this.resetForm();
                axios.post("/role/findAll.do").then(res => {
                    if (res.data.flag) {
                        this.tableData = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.dialogFormVisible4Edit = true;
                this.activeName = 'first';
                // this.formData=row;
                axios.post("/user/findById.do?id=" + row.id).then(res => {
                    if (res.data.flag) {
                        this.formData = res.data.data;
                        this.formData.password = null;
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
                axios.post("/role/findAll.do").then(res => {
                    if (res.data.flag) {
                        this.tableData = res.data.data;
                        axios.post("/user/findRoleIdsByUserId.do?id=" + row.id).then(res => {
                            if (res.data.flag) {
                                this.roleIds = res.data.data;
                            } else {
                                this.$message.error(res.data.message);
                            }
                        });
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                this.$confirm("你确定要删除吗？", "提示", {type: 'warning'}).then(() => {
                    axios.post("/user/delete.do?id=" + row.id).then(res => {
                        if (res.data.flag) {
                            this.$message({
                                type: 'success',
                                message: res.data.message
                            });
                        } else {
                            this.$message.error(res.data.message);
                        }
                    }).catch(r => {
                        this.showMessage(r);
                    }).finally(() => {
                        this.findPage();
                    });
                }).catch(() => {
                    this.$message({
                        message: "操作已取消！",
                        type: 'info'     //设置类型为消息提示框
                    });
                });
            }
        }
    })
</script>
</html>
